package com.ict.ms.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ict.ms.abnormal.facade.common.SplitUtils;
import com.ict.ms.artificial.facade.api.IArtificialService;
import com.ict.ms.artificial.facade.api.IAttributeService;
import com.ict.ms.artificial.facade.model.AppointTarget;
import com.ict.ms.artificial.facade.model.Attribute;
import com.ict.ms.artificial.facade.model.AttributeDto;
import com.ict.ms.artificial.facade.model.Config_Fusion;
import com.ict.ms.artificial.facade.model.FuseTargetFromSource;
import com.ict.ms.artificial.facade.model.UpdateTarget;
import com.ict.ms.common.StateMsg;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by chenlong on 2016/12/20.
 */

/**
 * 人工干预
 */
@Controller
@RequestMapping("/artificial")
public class ArtificialController {
    @Resource
    IArtificialService artificialService;

    @Resource
    IAttributeService attributeService;

    /**
     * 目标合批
     * @param map
     * @param shipId1 船舶ID1
     * @param shipId2 船舶ID2
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "mix.do",method = RequestMethod.POST)
    public String mergeTarget(ModelMap map, @RequestParam(value = "shipid1",required = false) int shipId1
            ,@RequestParam(value = "shipid2",required = false) int shipId2){
        String msg;
        String jsonStr;

        AppointTarget appointTarget=artificialService.mergeTarget(shipId1,shipId2);
        if(appointTarget==null){
            msg="合批指令发送失败";
            map.addAttribute("result",msg);
            jsonStr = StateMsg.toJson(-1,map);
            return jsonStr;
        }
        jsonStr= StateMsg.toJsonObj(1,appointTarget);
        return jsonStr;
    }

    /**
     * 目标分批
     * @param map
     * @param shipId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "unmix.do",method = RequestMethod.POST)
    public String separateTarget(ModelMap map,@RequestParam(value = "shipid",required = false) int shipId){
        String msg;
        String jsonStr;

        List<FuseTargetFromSource> fuseTargetFromSourceList=artificialService.separateTarget(shipId);
        if(fuseTargetFromSourceList.size()==0){
            msg="分批指令发送失败";
            map.addAttribute("result",msg);
            jsonStr = StateMsg.toJson(-1,map);
            return jsonStr;
        }

//        msg="分批指令发送成功";
        map.addAttribute("list",fuseTargetFromSourceList);
        jsonStr= StateMsg.toJson(1, map);
        return jsonStr;
    }

    /**
     * 目标属性修改
     * @param map
     * @param shipId
     * @param shiptype
     * @param shipname
     * @param country
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "alter.do",method = RequestMethod.POST)
    public String updateTarget(ModelMap map,
                               @RequestParam(value = "shipId",required = false) int shipId
                                ,@RequestParam(value = "shiptype",required = false) int shiptype,
                               @RequestParam(value = "shipname",required = false) String shipname
                                ,@RequestParam(value = "country",required = false) String country){

        String msg;
        String jsonStr;
        UpdateTarget updateTarget=new UpdateTarget(shipId,shiptype,shipname,country);

        AppointTarget appointTarget=artificialService.updateTarget(updateTarget);
        if(appointTarget==null){
            msg="目标属性修改指令发送失败";
            map.addAttribute("result",msg);
            jsonStr = StateMsg.toJson(-1,map);
            return jsonStr;
        }

        jsonStr= StateMsg.toJsonObj(1, appointTarget);
        return jsonStr;
    }

    /**
     * 消批设置获取
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getbatchelimination.do",method = RequestMethod.GET)
    public String getBatchElimination(ModelMap map){
        String msg;
        String jsonStr;

        Config_Fusion config_fusion=artificialService.getBatchElimination();
        if (config_fusion==null){
            msg="获取失败";
            map.addAttribute("result",msg);
            jsonStr = StateMsg.toJson(-1,map);
            return jsonStr;
        }

        jsonStr = StateMsg.toJsonObj(1,config_fusion);
        return jsonStr;
    }

    /**
     * 根据信息类型进行消批设置
     * @param map
     * @param timeSet
     * @param typeSet
     * @param userName
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "setBatchEliminationForType.do",method = RequestMethod.POST)
    public String setBatchEliminationForType(ModelMap map
            ,@RequestParam(value = "timeSet",required = false) String timeSet
            ,@RequestParam(value = "typeSet",required = false) String typeSet
            ,@RequestParam(value = "userName",required = false) String userName){
        String msg;
        String jsonStr;

        try{
            String[] timeSetArray=SplitUtils.MDVNumSplit_StrArray(timeSet);
            String[] typeSetArray=SplitUtils.MDVNumSplit_StrArray(typeSet);
            boolean ifSuc=artificialService.setBatchEliminationForType(timeSetArray,typeSetArray,userName);
            if(!ifSuc){
                msg="设置失败";
                map.addAttribute("result",msg);
                jsonStr = StateMsg.toJson(-1,map);
                return jsonStr;
            }

            boolean ifMQSuc=artificialService.setMQBatchEliminationForType(timeSetArray,typeSetArray,userName);
            if(!ifMQSuc){
                msg="设置成功，但发送命令失败";
                map.addAttribute("result",msg);
                jsonStr = StateMsg.toJson(-2,map);
                return jsonStr;
            }

            msg="设置成功，发送MQ消息队列成功";
            map.addAttribute("result",msg);
            jsonStr = StateMsg.toJson(1,map);
            return jsonStr;
        }catch (Exception e){
            System.out.println("参数处理错误："+e);
            msg="设置失败,参数处理错误";
            map.addAttribute("result",msg);
            jsonStr = StateMsg.toJson(-1,map);
            return jsonStr;
        }
    }

    /**
     * 获取不同信息类型的消批时间
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getBatchEliminationList.do",method = RequestMethod.GET)
    public String getBatchEliminationList(ModelMap map){
        String msg;
        String jsonStr;

        List<Config_Fusion> config_fusionList=artificialService.getBatchEliminationList();
        if (config_fusionList.size()==0||config_fusionList.size()<1){
            msg="获取失败";
            map.addAttribute("result",msg);
            jsonStr = StateMsg.toJson(-1,map);
            return jsonStr;
        }

        map.addAttribute("list",config_fusionList);
        jsonStr = StateMsg.toJson(1,map);
        return jsonStr;
    }

    /**
     * 所有目标属性修改记录查询
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getAttributeList.do",method = RequestMethod.GET)
    public String getAttributeList(ModelMap map){
        String msg;
        String jsonStr;

        List<Attribute> attributes=attributeService.getAttributes();
        if (attributes==null||attributes.size()<1){
            msg="获取失败";
            map.addAttribute("result",msg);
            jsonStr = StateMsg.toJson(-1,map);
            return jsonStr;
        }

        map.addAttribute("list",attributes);
        jsonStr=StateMsg.toJson(1,map);
        return jsonStr;
    }

    /**
     * 单目标属性修改项查询
     * @param map
     * @param id 目标ID
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getAttributeChange.do",method = RequestMethod.POST)
    public String getAttributeChange(ModelMap map,@RequestParam(value = "id",required = false) int id){
        String msg;
        String jsonStr;

        AttributeDto dto=attributeService.getAttributeChange(id);
        jsonStr = StateMsg.toJsonObj(1, dto);
        return jsonStr;
    }

    /**
     * 所有目标属性修改项查询
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getAttributeChangeList.do",method = RequestMethod.GET)
    public String getAttributeChangeList(ModelMap map){
        String msg;
        String jsonStr;

        List<AttributeDto> dtos=attributeService.getAttributeChangeList();
        if (dtos==null||dtos.size()<1){
            msg="获取失败";
            map.addAttribute("result",msg);
            jsonStr = StateMsg.toJson(-1,map);
            return jsonStr;
        }

        map.addAttribute("list",dtos);
        jsonStr=StateMsg.toJson(1, map);
        return jsonStr;
    }


    /**
     * 撤销属性修改
     * @param map
     * @param id 撤销属性修改的目标ID
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "revocation.do",method = RequestMethod.POST)
    public String revocation(ModelMap map,@RequestParam(value = "id",required = false) int id){
        String msg;
        String jsonStr;

        Boolean done=attributeService.revocation(id);
        if(!done){
            msg="属性撤销失败";
            map.addAttribute("result",msg);
            jsonStr = StateMsg.toJson(-1,map);
            return jsonStr;
        }
        msg="属性撤销成功";
        map.addAttribute("result",msg);
        jsonStr = StateMsg.toJson(1,map);
        return jsonStr;
    }
}